﻿using Microsoft.Extensions.Configuration;
using System.IO;

namespace Gaea.CodeFirst.Test
{
    public abstract class JsonSettingsConfigurationProvider : IJsonSettingsConfigurationProvider
    {
        public IConfigurationRoot ConfigurationRoot
        {
            get; private set;
        }

        public virtual IConfigurationRoot LoadJsonFile(string environmentName)
        {
            var jsonFileName = $"appsettings.{environmentName}.json";
            var jsonFilePath = Path.Combine(Directory.GetCurrentDirectory(), jsonFileName);
            if (File.Exists(jsonFilePath))
            {
                ConfigurationRoot = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(jsonFileName, optional: true, reloadOnChange: true).Build();
                return ConfigurationRoot;
            }
            throw new FileNotFoundException(jsonFilePath);
        }
    }
}
